package com.qj.yantaishan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qj.yantaishan.common.core.controller.BaseController;
import com.qj.yantaishan.common.core.domain.R;
import com.qj.yantaishan.common.utils.StringUtils;
import com.qj.yantaishan.service.IOrderService;
import com.qj.yantaishan.service.IOrderVoService;
import com.qj.yantaishan.vo.FtOrderVo;
import com.qj.yantaishan.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 订单Controller
 *
 * @author qihe
 * @date 2022-07-22
 */
@RestController
@RequestMapping("/yantaishan/Order")
public class OrderController extends BaseController {
    private String prefix = "yantaishan/Order";

    @Autowired
    private IOrderVoService orderVoService;
    @Autowired
    private IOrderService orderService;

    @GetMapping("test/{id}")
    public R getOrder(@PathVariable Integer id) {
        return R.ok(orderService.getById(id));
    }

    @GetMapping("list")
    public R getOrderList(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
                          @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                          @RequestParam(required = false, defaultValue = "0") Integer status) {

        Page<OrderVo> orderVoPage = new Page<OrderVo>(currentPage, pageSize);
        QueryWrapper<OrderVo> queryWrapper = new QueryWrapper<>();
        if (status == 0) {

        } else {
            queryWrapper.eq("status", status);
        }
        Page<OrderVo> page = orderVoService.page(orderVoPage, queryWrapper);
        return R.ok(page);
    }

    @GetMapping()
    public R getOrderDetail(@RequestParam(required = true) Integer orderId) {
        OrderVo orderVo = orderVoService.getOrderDetail(orderId);
        return R.ok(orderVo);
    }

    @PutMapping("update/star")
    public R submitScore(@RequestParam(required = true) Integer orderId, @RequestParam(required = true) Integer star) {
        OrderVo order = orderVoService.getById(orderId);
        if (order != null) {
            order.setStar(star);
        }
        orderVoService.updateById(order);
        return R.ok("成功");
    }

    @PutMapping("update/bak")
    public R submitBak(@RequestParam(required = true) Integer orderId, @RequestParam(required = true) String bak) {
        OrderVo order = orderVoService.getById(orderId);
        if (order != null) {
            order.setBak(bak);
            orderVoService.updateById(order);
        }

        return R.ok("成功");
    }

    @PutMapping("update/address")
    public R submitAddress(@RequestParam(required = true) Integer orderId, @RequestBody Map<String, String> address) {
        OrderVo order = orderVoService.getById(orderId);
        if (order != null) {
            final String address1 = address.get("address");
            final String addressUser = address.get("addressUser");
            final String addressTel = address.get("addressTel");
            if (StringUtils.isNotEmpty(address1)) {
                order.setAddress(address1);
            }
            if (StringUtils.isNotEmpty(addressUser)) {
                order.setAddressUser(addressUser);
            }
            if (StringUtils.isNotEmpty(addressTel)) {
                order.setAddressTel(addressTel);
            }
            orderVoService.updateById(order);
            return R.ok();

        }
        return R.ok("成功");
    }

    @PutMapping("update/price")
    public R updatePrice(@RequestParam(required = true) Integer orderId, @RequestBody Map<String, String> priceMap) {
        OrderVo orderVo = orderVoService.getById(orderId);
        if (orderVo != null) {
            //邮费
            String postagePrice = priceMap.get("postagePrice");
            if (StringUtils.isNotEmpty(postagePrice)) {
                BigDecimal postagePrice1 = new BigDecimal(postagePrice);
                orderVo.setPostage(postagePrice1);
                orderVo.setTotal(orderVo.getTotal().add(postagePrice1));
            }
            //加减价格
            String orderPrice = priceMap.get("orderPrice");
            if (StringUtils.isNotEmpty(orderPrice)) {
                BigDecimal floatAmount = new BigDecimal(orderPrice);
                orderVo.setFloatAmount(floatAmount);
                orderVo.setTotal(orderVo.getTotal().add(floatAmount));
            }
            orderVoService.updateById(orderVo);
            return R.ok();

        }

        return R.fail("找不该订单");
    }

    @PutMapping("cancel")
    public R cancelOrder(@RequestParam(required = true) Integer orderId) {
        OrderVo orderVo = orderVoService.getById(orderId);
        /**
         * 取消订单
         * 1.修改状态  status 5
         * 2.取消方式 cancelMethod 1卖家手动取消
         * 3.取消时间   时间戳
         */
        if (orderVo != null) {
            orderVo.setStatus(5);
            orderVo.setCancelMethod(1);
            orderVo.setCancelTime(new Date().getTime() / 1000);
            orderVoService.updateById(orderVo);
            return R.ok();
        }
        return R.fail("找不该订单");

    }

    @GetMapping("ftOrder/list")
    public R getFtOrderList(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
                            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Map<String, Object> ftOrderVos = orderVoService.getFtOrderList(currentPage, pageSize);
        return R.ok(ftOrderVos);
    }

    @GetMapping("ftOrder/detail")
    public R getFtOrderDetail(@RequestParam(required = true) Integer orderId) {
        FtOrderVo ftOrderVo = orderVoService.getFtOrderDetail(orderId);
        return R.ok(ftOrderVo);
    }

    @PutMapping("save/ftOrder")
    public R saveFtOrder(@RequestBody Map<String, String> ftBody) {
        String certPath = ftBody.get("certPath");
        String videoPath = ftBody.get("videoPath");
        String orderId = ftBody.get("orderId");
        int flag = orderVoService.saveFtOrder(certPath, videoPath, orderId);
        return R.ok();
    }

    @PutMapping("update/ftStatus")
    public R updateFtStatus(@RequestParam(required = true) Integer orderId, @RequestParam(required = true) Integer status) {
        //status ==订单状态   1==未支付    2==已支付
        OrderVo orderVo = orderVoService.getById(orderId);
        if (orderVo != null) {
            orderVo.setStatus(status);
            orderVoService.updateById(orderVo);
            return R.ok();
        }
        return R.fail();
    }

    @GetMapping("overview")
    public R getOrderOverview() {
        Map<String, Object> result = orderVoService.getOrderOverview();
        return R.ok(result);
    }

    @GetMapping("/selffetch")
    //
    public R getSelffetchList(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                              @RequestParam(required = false, defaultValue = "0") Integer status) {
        LambdaQueryWrapper<OrderVo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderVo::getShippingMethod, "selffetch");
        Page<OrderVo> orderVoPage = new Page<>(currentPage, pageSize);
        if (status != 0) queryWrapper.eq(OrderVo::getStatus, status);
        return R.ok(orderVoService.page(orderVoPage, queryWrapper));
    }


    @GetMapping("/codpay")
    //
    public R getCodpayList(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
                           @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                           @RequestParam(required = false, defaultValue = "0") Integer status) {
        LambdaQueryWrapper<OrderVo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderVo::getPaymentMethod, "codpay");
        Page<OrderVo> orderVoPage = new Page<>(currentPage, pageSize);
        if (status != 0) queryWrapper.eq(OrderVo::getStatus, status);
        return R.ok(orderVoService.page(orderVoPage, queryWrapper));
    }

    @GetMapping("star")
    public R getStarList(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
                         @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                         @RequestParam(required = false, defaultValue = "0") Integer status) {
        LambdaQueryWrapper<OrderVo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(OrderVo::getStar, 0);
        Page<OrderVo> orderVoPage = new Page<>(currentPage, pageSize);
        if (status != 0) queryWrapper.eq(OrderVo::getStatus, status);
        return R.ok(orderVoService.page(orderVoPage, queryWrapper));
    }
}
